package com.jiai.cloud.community.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.jiai.cloud.base.exception.ServiceException;
import com.jiai.cloud.base.page.CustomPage;
import com.jiai.cloud.base.tools.RestResultGenerator;
import com.jiai.cloud.community.entity.TemplateSearch;
import com.jiai.cloud.community.service.ITTemplateService;
import com.jiai.cloud.community.entity.TTemplate;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 佳爱网络科技
 * @since 2017-06-07
 */
@RestController
@RequestMapping("/api/v1/tTemplate")
public class TTemplateController {
        @Autowired
        private ITTemplateService emplateService;
        //添加模块
        @RequestMapping(value = "/addTemplate" ,method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"})
        public Object addTemplate(TTemplate TTemplate) throws ServiceException{
            try {
                TTemplate.setTime(new Date());
                emplateService.insertOrUpdate(TTemplate);
                return RestResultGenerator.genSuccessResult();
            } catch (Exception e) {
                throw new ServiceException("编辑失敗", e.getMessage());
            }
        }
        //查询模版
    @RequestMapping(value = "/findByAll" ,method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"})
    public Object findByAll(TemplateSearch search) throws ServiceException {
        try {
            Page page=search.getPagePlus();
            emplateService.selectPage(page);
            return RestResultGenerator.genSuccessResult(new CustomPage(page));
        } catch (Exception e) {
            throw new ServiceException("查询失败", e.getMessage());
        }
    }
    //删除模版
    @RequestMapping(value = "/del", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public Object deleteId( Integer id) throws ServiceException{
        try{
            emplateService.deleteById(id);
            return RestResultGenerator.genSuccessResult();
        }catch (Exception e){
            throw new ServiceException("删除失敗", e.getMessage());
        }
    }
    //通过id查
    @RequestMapping(value = "/findById", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public Object findById(Integer id) throws ServiceException {
        try {
            emplateService.selectById(id);
            return RestResultGenerator.genSuccessResult();
        } catch (Exception e) {
            throw new ServiceException("删除失敗", e.getMessage());
        }
    }
    //改
    @RequestMapping( value = "/updateTemplate" ,method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"})
    public  Object updateTemplate(TTemplate template) throws ServiceException{
        try {

            return RestResultGenerator.genSuccessResult(emplateService.updateById(template));
        } catch (Exception e) {
            throw new ServiceException("删除失敗", e.getMessage());
        }
    }
}
